import java.sql.Statement;
import java.util.List;

/**
 * @projectName: LeetCode
 * @package: PACKAGE_NAME
 * @className: Point2
 * @author: Mocrash
 * @description: TODO
 * @date: 2024/10/14 10:12
 * @version: 1.0
 */
public class Point2 {
    public static void main(String[] args) {
        int[] num1Arr = {1, 2, 3, 4, 5, 6, 7};
        int[] num2Arr = {9, 7, 8, 2, 3, 4, 5, 6, 1};
        ListNode num1 = ListNode.ofArray(num1Arr);
        ListNode num2 = ListNode.ofArray(num2Arr);
        Solution solution = new Solution();
        ListNode res = solution.addTwoNumbers(num1, num2);
        ListNode.forEachPrintout(res);
    }

    static class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode ret = new ListNode();
            ListNode tmp = ret;
            while (l1 != null && l2 != null){
                tmp.next = new ListNode(l1.val + l2.val);
                tmp = tmp.next;
                l1 = l1.next;
                l2 = l2.next;
            }
            while (l1 != null){
                tmp.next = new ListNode(l1.val);
                tmp = tmp.next;
                l1 = l1.next;
            }
            while (l2 != null){
                tmp.next = new ListNode(l2.val);
                tmp = tmp.next;
                l2 = l2.next;
            }

            tmp = ret.next;
            while (tmp != null){
                if (tmp.val > 9){
                    if (tmp.next == null){
                        tmp.next = new ListNode(0);
                    }
                    tmp.next.val = tmp.next.val + tmp.val / 10;
                    tmp.val = tmp.val % 10;
                }
                tmp = tmp.next;
            }
            return ret.next;
        }
    }
}

class ListNode {
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
    public static ListNode ofArray(int[] array){
        ListNode ret = new ListNode();
        ListNode tmp = ret;
        for (int i = 0; i < array.length; i++) {
            tmp.next = new ListNode(array[i]);
            tmp = tmp.next;
        }
        return ret.next;
    }

    public static void forEachPrintout(ListNode list){
        ListNode tmp = list;
        while (tmp != null){
            System.out.println(tmp.val);
            tmp = tmp.next;
        }
    }
}
